package com.bcu.wish.controller;

import com.bcu.wish.entity.Message;
import com.bcu.wish.entity.User;
import com.bcu.wish.entity.Wishfill;
import com.bcu.wish.entity.search.UserSearch;
import com.bcu.wish.entity.search.WishfillSearch;
import com.bcu.wish.service.TimeService;
import com.bcu.wish.service.WishfillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Api(tags = "志愿模块")
@RestController
@RequestMapping("/api/wish")
public class WishfillController {
    @Resource
    private WishfillService wishfillService;

    @Resource
    private TimeService timeService;

    @ApiOperation(value = "学生分专业.")
    @PostMapping(value = "/result")
    public Message<List<User>> StudentDivided() {

        return wishfillService.StudentDivided();
    }

    @ApiOperation(value = "填报志愿")
    @PostMapping(value = "/")
    public Message insert(Wishfill wishfill) throws ParseException {
        //填报志愿截止时间，id=2
        Date begindate = timeService.selectByPrimaryKey(7).getObj().getTimeLimittime(); //开始填报志愿时间
        Date stopdate = timeService.selectByPrimaryKey(8).getObj().getTimeLimittime();  //截止填报志愿时间
        Date nowdate = new Date();
        if (nowdate.after(begindate)) {
            if(nowdate.before(stopdate)) {
                wishfill.setWishPostTime(nowdate);
                return wishfillService.insertSelective(wishfill);
            }
            return  Message.fail("填报志愿时间已截止！");
        }
        return Message.fail("填报志愿时间未开始！");

    }

    @ApiOperation(value = "根据userid查看填报志愿信息")
    @GetMapping(value = "/userid/one")
    public Message<Wishfill> selectByUserId(String userid){
        return wishfillService.selectByUserId(userid);
    }

    @ApiOperation(value = "根据志愿id修改志愿")
    @PutMapping(value = "/")
    public Message updateBywishFillId(Wishfill wishfill) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
        Date limitdate = simpleDateFormat.parse("2020-10-18 12:00:01");
        Date nowdate = new Date();
        if (nowdate.before(limitdate)){
            wishfill.setWishPostTime(nowdate);
            return wishfillService.updateByPrimaryKeySelective(wishfill);
        }
        return Message.fail("填报志愿时间已截止！");
    }

    @ApiOperation(value = "根据wishFillId查看填报志愿信息")
    @GetMapping(value = "/one/wishFillId")
    public Message<Wishfill> selectBywishFillId(int wishFillId){
        return wishfillService.selectByPrimaryKey(wishFillId);
    }

    @ApiOperation(value = "查看所有填报志愿信息")
    @GetMapping(value = "/all")
    public Message<List<Wishfill>> selectAll(){
        return wishfillService.selectAll();
    }

    @ApiOperation(value = "导出所有填报数据Excel,返回url")
    @GetMapping(value = "/Excel")
    public Message getExcel(){
        return wishfillService.getExcel();
    }

    @ApiOperation(value = "根据学号导出学生填报数据Word,返回url")
    @GetMapping(value = "/Word")
    public Message getWord(String userId) throws IOException {
        return wishfillService.getWord(userId);
    }

    @ApiOperation(value = "英语排序")
    @GetMapping(value = "/sort")
    public Message lalalal(String s){
        return wishfillService.lalalal(s);
    }

    @ApiOperation(value = "模糊条件搜索")
    @GetMapping(value = "/search")
    public Message search(WishfillSearch wishfillSearch){
        return wishfillService.search(wishfillSearch);
    }
}
